package com.hz.sdka.ada;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;

import com.anythink.rewardvideo.api.ATRewardVideoAd;
import com.hz.sdka.commona.SDKContext;
import com.hz.sdka.commona.api.reward.OnHZRewardVideoListener;
import com.hz.sdka.commona.bean.HZAdError;
import com.hz.sdka.commona.bean.HZAdInfo;
import com.hz.sdka.sdka.AdRewardVideoa;

/**
 * topon  激励视屏
 */
public class RewardedVideoa extends AdBasea {
    private static final RewardedVideoa rewardedVideo = new RewardedVideoa();
    private String TAG = "TopOnAd";
    private AdRewardVideoa hzRewardVideoAd;

    private Handler handler = new Handler(Looper.getMainLooper());
    private AdListenera adListener;

    public static RewardedVideoa getInstance() {
        return rewardedVideo;
    }


    public void showAd(Activity activity, String placeId, String scenarioId) {
        if (!TextUtils.isEmpty(scenarioId)) {
            ATRewardVideoAd.entryAdScenario(placeId, scenarioId);
        }
        if (adListener != null) adListener.adRequest();
        if (hzRewardVideoAd != null && hzRewardVideoAd.isReady()) {
            adListener.adReady();
            hzRewardVideoAd.show(activity, scenarioId);
        } else {
            load(placeId);
        }
    }

    public double getEcpm() {
        if (hzRewardVideoAd != null) {
            hzRewardVideoAd.getEcpm();
        }
        return 0.00;
    }


    /*判断广告是否准备好*/
    public boolean isReady() {
        if (hzRewardVideoAd != null)
            return hzRewardVideoAd.isReady();
        else return false;
    }

    public void load(String placeId) {
        if (hzRewardVideoAd == null)
            hzRewardVideoAd = new AdRewardVideoa(SDKContext.getInstance().getContext(), placeId);
        hzRewardVideoAd.setListener(new OnHZRewardVideoListener() {
            @Override
            public void onRewardVideoLoadFailed(HZAdError hzAdError) {
                Log.i(TAG, "onRewardVideoLoadFailed:\n" + hzAdError.printStackTrace());
                if (adListener != null) adListener.adError(hzAdError);
            }

            @Override
            public void onRewardVideoLoaded() {
                Log.i(TAG, "onRewardVideoLoaded");
                if (adListener != null) adListener.adLoaded();
            }

            @Override
            public void onRewardVideoShown(HZAdInfo hzAdInfo) {
                Log.i(TAG, "onRewardedVideoAdShow:\n");
                if (adListener != null) adListener.adShow();
            }

            @Override
            public void onRewardVideoShowFailed(HZAdError hzAdError) {
                Log.i(TAG, "onRewardedVideoAdFailed:\n" + hzAdError.printStackTrace());
                if (adListener != null) adListener.adError(hzAdError);
            }

            @Override
            public void onRewardVideoPlayComplete() {
                Log.i(TAG, "onRewardVideoPlayComplete:");
                if (adListener != null) adListener.adComplete();
            }

            @Override
            public void onRewardVideoPlayError() {
                Log.i(TAG, "onRewardedVideoAdPlayFailed:\n");
                if (adListener != null)
                    adListener.adError(new HZAdError("", "onRewardVideoPlayError", "", ""));
            }

            @Override
            public void onRewardVideoClosed() {
                Log.i(TAG, "onRewardedVideoAdClose:\n");
                if (adListener != null) adListener.adClose();
                load(placeId);
            }

            @Override
            public void onRewardVideoClicked() {

            }

            @Override
            public void onReward() {

            }
        });
        load();
    }

    public void setAdListener(AdListenera adListener) {
        this.adListener = adListener;
    }

    /**
     * 加载广告
     */
    private void load() {
        if (hzRewardVideoAd != null) {
            if (adListener != null) adListener.adLoading();
            if (!isReady()) hzRewardVideoAd.load();
        }
    }

    /**
     * 延迟load
     */
    private void postLoad() {
        if (hzRewardVideoAd != null && !isAdPostLoading) {
            isAdPostLoading = true;
            handler.postDelayed(() -> {
                if (!isReady()) {
                    isAdPostLoading = false;
                    load();
                }
            }, 20 * 1000);
        }
    }
}
